package com.example.moboile2032021606168xmy0518classtest;

import androidx.appcompat.app.AppCompatActivity;


import android.app.VoiceInteractor;
import android.os.Bundle;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private TextView tvMainTitle;
    private TextView tvMainContent;
    String url = "http://192.168.29.230:3000/profile";
    OkHttpClient okHttpClient = new OkHttpClient();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvMainContent = findViewById(R.id.tv_main_content);

       // new Runnable();
        new Thread(new Runnable() {
            @Override
            public void run() {


                Request request = new Request.Builder()
                        .url(url)
                        .build();
                Call call = okHttpClient.newCall(request);
                try {
                    Response response = call.execute();

                    String result = response.body().string();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                JSONObject jsonObject =new JSONObject(result);
                                String name = jsonObject.optString("name");
                                tvMainContent.setText(name);
                            }catch (JSONException e){
                                e.printStackTrace();
                            }
                            tvMainContent.setText(result);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }



    private void initView(){
        tvMainTitle = (TextView) findViewById(R.id.tv_main_title);
        tvMainContent = (TextView) findViewById(R.id.tv_main_content);
    }
}